package com.study.method;

public class MethodTest1 {
    public static void main(String[] args) {
        // 需求：使用方法计算1~n之间的和
        System.out.println(sum1(100));
        System.out.println(sum2(100));
        System.out.println(sum1(5));
        System.out.println(sum2(5));
        System.out.println(sum1(9));
        System.out.println(sum2(9));
    }

    public static int sum1(int n){
        int res = 0;
        for (int i = 1; i <= n; i++) {
            res += i;
        }
        return res;
    }

    public static int sum2(int n){
        int x = n/2;
        return n%2 == 0 ? x*(1+n) : x*(1+n)+x+1;
    }
}
